﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;


namespace DevMaker.CM.DAO {
    public class CuponDao {
        public Cupon ObtieneCupon(String codigo) {
            Cupon cupon = null;
            var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CM_Connection"].ToString());
            try {
                conn.Open();
                var query =
                    String.Format(
                        @"SELECT  top 1 *
                        FROM viewCabBookingCMK
                        WHERE vwBookingH_strStatus='P' AND vwBookingD_strStatus ='P' and (vwPrice_strCode like '%OVS%' or vwPrice_strCode like '%WEB%')
                        AND vwBookingH_strBookingId=@codigo
                        ORDER BY vwBookingD_intSequence desc,vwSessionScreeningTime DESC");
                var descParam = new SqlParameter("@codigo", SqlDbType.VarChar, 100);
                descParam.Value = codigo;
                var cmd = new SqlCommand(query, conn);
                cmd.Parameters.Add(descParam);

                cmd.Prepare();
                var reader = cmd.ExecuteReader();
                while (reader.Read()) {
                    cupon = new Cupon();
                    cupon.Codigo = reader["vwBookingH_strBookingId"].ToString();
                    cupon.FechaFuncion = Convert.ToDateTime(reader["vwSessionScreeningTime"]);
                    cupon.ClienteEmail = reader["vwBookingH_strEmail"].ToString();
                    cupon.ClienteNombre = reader["vwBookingH_strName"].ToString();
                    cupon.Pelicula = reader["vwFilm"].ToString();
                    cupon.Sala = reader["vwScreenName"].ToString();
                    cupon.CantidadEntradas = Convert.ToInt64(reader["vwBookingD_intSequence"]);

                    cupon.ImpresoEn = reader["vwBookingH_strPickupWorkstn"].ToString();
                    cupon.ImpresoPor = reader["vwBookingH_intPickupUser"].ToString();
                    if (!String.IsNullOrEmpty(reader["vwBookingH_dtmDateCollected"].ToString()))
                        cupon.FechaImpresion = Convert.ToDateTime(reader["vwBookingH_dtmDateCollected"]);
                }

            } catch (Exception e) {
                Console.Write(e.Message);
            } finally {
                if (conn.State != ConnectionState.Closed)
                    conn.Close();

            }
            return cupon;
        }
    }

}
